<?php
//模块名_控制名
class Product_QcController extends Zend_Controller_Action
{
	public function preDispatch(){
		$this->tplDirectory = "product/quality/";
	}

	public function indexAction(){
		if($this->_request->isPost()){
			$qco_code = trim($this->_request->getParam("qco_code",""));
			$receiving_code = trim($this->_request->getParam("receivingNo",""));
			$customer_id = trim($this->_request->getParam("customer_id",""));
			$dateFrom = trim($this->_request->getParam("dateFrom",""));
			$status = trim($this->_request->getParam("status",""));
			$dateTo = trim($this->_request->getParam("dateTo",""));
			$page = trim($this->_request->getParam("page",1));
			$pageSize = trim($this->_request->getParam("pageSize",20));
			$condition = array(
					"qco_ref_code" => $receiving_code,
					'qco_status'=>$status,
					"qco_code" => $qco_code,
					'customer_id'=>$customer_id,
					"date_from" => $dateFrom,
					"date_to" => $dateTo,
				);
            if($page=='' || $page==0 ){
               $page=1;
            }
			$total = Product_Service_QualityControlOrder::getByCondition($condition,"count(*)");
			$list = Product_Service_QualityControlOrder::getByCondition($condition,"*",'qco_create_time desc',$pageSize,$page);
			//产品信息
			$proServiceObj = new Product_Service_Product();
			foreach ($list as $key => $val){
				$qciPros = array();
				$qciPros = Product_Service_QualityControlItem::getQcoCodeAll($val['qco_code']);
				$receiving = Product_Service_Receiving::getByReceivingCode($val['qco_ref_code']);
				
				
				foreach ($qciPros as $k =>$v){
					$recevingItemRes = Product_Service_ReceivingItem::getByReceivingAsnPid(array('product_id'=>$v['product_id'],'receiving_code'=>$val['qco_ref_code']));
					$productInfo = $proServiceObj->getProductInfo($v['product_id']);
					$qciPros[$k]['SKUNo'] = $productInfo['product_sku'];
					$qciPros[$k]['SKUName'] = $productInfo['product_title'];
					$qciPros[$k]['weight'] = $productInfo['product_weight'];
					$qciPros[$k]['category'] = $productInfo['category']['poc_value'];
					$list[$key]['ri_sort'] = $recevingItemRes['ri_sort'];
					$list[$key]['is_qc'] = $recevingItemRes['is_qc'];
					$list[$key]['order_type']=$receiving['order_type'];
					
					$list[$key]['contact'] = $receiving['contact'];						//联系人
					$list[$key]['contact_mobile'] = $receiving['contact_mobile'];		//联系电话
		        }
				$list[$key]['qciPros']=$qciPros;
			}
			
			
			$return = array("ask" =>0,"total" => $total);
			if(!empty($list)){
					$return['ask'] = 1;
					$return['result'] = $list;
			}
			die(json_encode($return));
		}
        $categorys = Product_Service_Product::getAllCategory();
        $this->view->categorys = $categorys;
		$this->view->status=Product_Service_QualityControlOrder::$statusText;
		echo Cff::renderTpl($this->tplDirectory.'index.tpl',"layout");
	}
	
	public function operationAction(){
		$qco_code = trim($this->_request->getParam("code",""));
		$qcoRes = Product_Service_QualityControlOrder::getQcoCode($qco_code);
		
		$qciRows=Product_Service_QualityControlItem::getQcoCodeAll($qco_code);
		if($qciRows){
				
			$cond=array(
					"product_id"=>$qciRows[0]['product_id'],
					"receiving_code"=>$qcoRes['qco_ref_code']
						
			);
			$asnRow=Product_Service_ReceivingItem::getByReceivingAsnPid($cond);
		}else{
			die('找不到相关的收货单');
		}
		
		if($qcoRes['qco_status']==2){
			$results = Product_Service_QualityControlOrder::getDetailResult($qco_code);
			$this->view->results=$results;
			//查看是否QC
			if($asnRow['is_qc']){
				$this->_redirect('/product/qc/detail/code/'.$qco_code);
			}else{
				$this->_redirect('/product/qc/exdetail/code/'.$qco_code);
			}

		}else{

			$res = Product_Service_QualityControlOrder::getOperationResult($qco_code);
			$this->view->res=$res;
			$this->view->problem_type=Product_Service_QualityControlOrder::$problem_type;
			//查看是否QC
			if($asnRow['is_qc']){
				echo $this->view->render('product/quality/operation.tpl');
			}else{
				echo $this->view->render('product/quality/exemption.tpl');
			}
			
			
			
		}
	}
	
	public function qcConfirmAction(){
		$params = $this->_request->getParams();

		$return['ask'] = 0;
		$result=Product_Service_QualityControlOrder::qcConfirmService($params);
		if($result['ask']){
			$return['ask']	= 1 ;
		}else{
			$return['ask']	= 0 ;
			$return['msg']	= $result['msg'] ;
		}
		//die(json_encode($return));
		die(Zend_Json::encode($return));
	}
	
	public function detailAction(){
		$qco_code = trim($this->_request->getParam("code",""));
		
		$results = array();
		$results[] = Product_Service_QualityControlOrder::getDetailResult($qco_code);
		$this->view->results=$results;
		
		$header = trim($this->_request->getParam("header","1"));
		$this->view->header=$header;
		$this->view->receiving_code=$results[0]['receiving']['receiving_code'];
// 		print_r($results);exit;
		echo $this->view->render('product/quality/detail.tpl');
	}
	
	public function exdetailAction(){
		$qco_code = trim($this->_request->getParam("code",""));
		$results = array();
		$results[] = Product_Service_QualityControlOrder::getDetailResult($qco_code);
		$this->view->results=$results;
		$this->view->receiving_code=$results[0]['receiving']['receiving_code'];
		
		echo $this->view->render('product/quality/exdetail.tpl');
	}
	
	public function barcodeAction(){
		Product_Service_Barcode::barcode($this->_request->code);
	}
	
	public function getQcProductAction(){
		$qcoidStr = $this->_request->getParam("qcoidStr","");
		$qcoidStr = preg_replace("/(.*),$/","\\1",$qcoidStr);
		$qcoids = explode(",",$qcoidStr);
		
		$proServiceObj = new Product_Service_Product();
		$products = array();
		foreach($qcoids as $k=>$v){
			$tempArr = array();	
			$qciRes = Product_Service_QualityControlItem::getQcoCodeAll($v);
			$qcoRow = Product_Service_QualityControlOrder::getQcoCode($v);
			foreach($qciRes as $qkey=>$qval){
				$productInfo = $proServiceObj->getProductInfo($qval['product_id']);
				$recevingItemRes = Product_Service_ReceivingItem::getByReceivingAsnPid(array('product_id'=>$qval['product_id'],'receiving_code'=>$qcoRow['qco_ref_code']));
				$tempArr[$qkey]['customer_id'] = $productInfo['customer_id'];
				$tempArr[$qkey]['product_id'] = $productInfo['product_id'];
				$tempArr[$qkey]['SKUNo'] = $productInfo['product_sku'];
				$tempArr[$qkey]['SKUName'] = $productInfo['product_title'];
				$tempArr[$qkey]['category'] = $productInfo['category']['poc_value'];
				$tempArr[$qkey]['received_qty'] = $recevingItemRes['received_qty'];
				$tempArr[$qkey]['order_qty'] = $recevingItemRes['order_qty'];
			}
			$products[$k]['product'] = $tempArr;
		}
		die(json_encode($products));
	}
	
	public function qcPrintLabelAction(){
		$params = $this->_request->getParams();
		$printType=$this->_request->getParam('type','5x2');
		$rows = array();
		foreach($params as $key => $param){
			if(strpos($key,"count") !== false){
				$rows[substr($key,strpos($key,"count")+5)] = $param;
			}
		}
		$proServiceObj = new Product_Service_Product();
		$results = $proServiceObj->getPrintLabelInfo($rows);
		
		if($printType=="5x2"){			
				$this->view->results = $results;
				echo Cff::renderTpl('product/product/print.tpl',"print");
		}else{
			$results = array_chunk($results,3);
			$this->view->resultsAll = $results;
			echo Cff::renderTpl('product/product/print_new.tpl',"print");
		}
		
		
		
	}
	
	public function printAllqcRecordAction(){
		$receiving_code = $this->_request->getParam('receiving_code','');
		$res = Product_Service_QualityControlOrder::getArrayByQcoRefCode($receiving_code);
		$results = array();
		$products = array();
		$count = 0;
		foreach($res as $val){
			$tem = Product_Service_QualityControlOrder::getDetailResult($val['qco_code']);
			$results[] = $tem;
			//为了显示头部图片 
			foreach($tem['product_info'] as $k=>$v){				
				$product = array();
				$product['count'] = ++$count;
				$product['product_id'] = $k;
				$product['product_id'] = $k;
				$product['appearance'] = "--";
				foreach($v as $v1){
					$product['product_sku'] = $v1["product_sku"];
					$product['customer_id'] = $v1["customer_id"];
					if($v1['poc_id']==15){
						$product['appearance']=$v1['po_value'];
					}
				}	
				$products[] = $product;
			}
		}
		
		$product_chunk = array_chunk($products, 24);//24张图片一页 分页
		$this->view->products=$product_chunk;
		
		// 		print_r($product_chunk);exit;
		
		$this->view->results=$results;
		$this->view->receiving_code=$receiving_code;
		
		$header = trim($this->_request->getParam("header","1"));
		$this->view->header=$header;
		
		echo $this->view->render('product/quality/detail.tpl');
	}

    /*
     * 导出QC报表页面
     */
    public function exportAction()
    {
        $this->view->exportType = Product_Service_QualityControlOrder::$exportType;
        echo Cff::renderTpl($this->tplDirectory . 'export.tpl', "layout");
    }

    /*
     * 导出QC报表数据
     */
    public function exportDataAction()
    {
        $exportType = $this->_request->getParam('exportType', '1');
        $startData = $this->_request->getParam('dateFrom', '');
        $endData = $this->_request->getParam('dateTo', '');
        if (!$this->_request->isPost()) {
            echo "操作失败!";
            exit;
        }
        $condition = array(
            'exportType' => $exportType,
            'startData' => $startData,
            'endData' => $endData,
        );
        if (empty($startData) || empty($endData)) {
            echo "操作失败,时间不能为空!";
            exit;
        }
        $filename = "QC_" . date("Y-m-d");
        header("Content-Disposition: attachment; filename=" . $filename . ".csv");
        header('Content-Type:APPLICATION/OCTET-STREAM');
        echo Product_Service_QualityControlOrder::exportData($condition);

    }

}